package cc.autoapi.pucong.auto.flow.provider.sys.controller;

import cc.autoapi.pucong.auto.flow.core.root.entity.UserJwt;
import cc.autoapi.pucong.auto.flow.core.root.exception.AutoFlowBaseController;
import cc.autoapi.pucong.auto.flow.core.root.exception.AutoFlowExceptionHandler;
import cc.autoapi.pucong.auto.flow.core.root.utils.Res;
import cc.autoapi.pucong.auto.flow.provider.sys.entity.SysReturnUrl;
import cc.autoapi.pucong.auto.flow.provider.sys.entity.UserLoginVo;
import cc.autoapi.pucong.auto.flow.provider.sys.service.SysLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * @author pucong
 * @company gitee.com/pucong
 * @create 2020-12-01
 */
@RestController
@RequestMapping("/sys/login")
public class SysLoginController extends AutoFlowBaseController implements AutoFlowExceptionHandler{

    @Autowired
    private SysLoginService sysLoginService;

    @GetMapping("/isLogin")
    public Res<Boolean> isLogin(HttpServletRequest request) {
        Boolean aBoolean = sysLoginService.isLogin(request);
        return Res.ok(aBoolean);
    }

    @GetMapping("/getLoginUser")
    public Res<UserJwt> getLoginUser(HttpServletRequest request) {
        UserJwt userJwt = sysLoginService.getLoginUser(request);
        return Res.ok(userJwt);
    }

    @PostMapping("/login")
    public Res<UserJwt> login(@Valid @RequestBody UserLoginVo userLoginVo) {
        UserJwt userJwt = sysLoginService.login(userLoginVo);
        return Res.ok(userJwt);
    }

    @GetMapping("/loginOut")
    public Res<SysReturnUrl> loginOut(HttpServletRequest request) {
        SysReturnUrl sysReturnUrl = sysLoginService.loginOut(request);
        return Res.ok(sysReturnUrl);
    }
}
